package com.ruoyi.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.base.domain.SSerial;
import com.ruoyi.base.service.ISSerialService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 流水号Controller
 *
 * @author ruoyi
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/base/serial")
public class SSerialController extends BaseController {

    @Autowired
    private ISSerialService sSerialService;

    /**
     * 查询流水号列表
     */
    @PreAuthorize("@ss.hasPermi('base:serial:list')")
    @GetMapping("/list")
    public TableDataInfo list(SSerial sSerial)
    {
        startPage();
        QueryWrapper<SSerial> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(sSerial.getModuleName()),SSerial::getModuleName,sSerial.getModuleName())
                .like(StringUtils.isNotEmpty(sSerial.getModuleCode()),SSerial::getModuleCode,sSerial.getModuleCode())
                .eq(StringUtils.isNotEmpty(sSerial.getCompanyCod()),SSerial::getCompanyCod,sSerial.getCompanyCod());

        List<SSerial> list = sSerialService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出流水号列表
     */
    @PreAuthorize("@ss.hasPermi('base:serial:export')")
    @Log(title = "流水号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SSerial sSerial)
    {

        QueryWrapper<SSerial> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(sSerial.getModuleName()),SSerial::getModuleName,sSerial.getModuleName())
                .like(StringUtils.isNotEmpty(sSerial.getModuleCode()),SSerial::getModuleCode,sSerial.getModuleCode())
                .eq(StringUtils.isNotEmpty(sSerial.getCompanyCod()),SSerial::getCompanyCod,sSerial.getCompanyCod());


        List<SSerial> list = sSerialService.list(queryWrapper);
        ExcelUtil<SSerial> util = new ExcelUtil<SSerial>(SSerial.class);
        util.exportExcel(response, list, "流水号数据");
    }

    /**
     * 获取流水号详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:serial:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sSerialService.getById(id));
    }

    /**
     * 新增流水号
     */
    @PreAuthorize("@ss.hasPermi('base:serial:add')")
    @Log(title = "流水号", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody SSerial sSerial) throws Exception {
        return sSerialService.insertSSerial(sSerial);
    }

    /**
     * 修改流水号
     */
    @PreAuthorize("@ss.hasPermi('base:serial:edit')")
    @Log(title = "流水号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SSerial sSerial)
    {
        return sSerialService.updateSSerial(sSerial);
    }

    /**
     * 删除流水号
     */
    @PreAuthorize("@ss.hasPermi('base:serial:remove')")
    @Log(title = "流水号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sSerialService.removeByIds(Arrays.asList(ids)));
    }



}
